package study.nosql.redis.support.redis;

import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.URL;

@Slf4j
@Component
public class RedissonConfig {

    private static final String CONFIG_FILE = "redisson-cfg.json";

    @Bean
    public RedissonClient redissonClient() throws IOException {
        URL url = RedissonConfig.class.getClassLoader().getResource(CONFIG_FILE);
        Config config = Config.fromJSON(url);
        return Redisson.create(config);
    }

    @Bean
    public RBloomFilter<String> bloomFilter(RedissonClient redissonClient) {
        RBloomFilter<String> bloomFilter = redissonClient.getBloomFilter("bloom:code");
        //初始化布隆过滤器，预期的插入数量为 1000000，期望误差率为 0.03
        bloomFilter.tryInit(5000L, 0.03);
        return bloomFilter;
    }
}
